<?php

defined('BASEPATH') OR exit('No direct script access allowed');

class Category extends CI_Controller {

    function __construct() {
        parent::__construct();
        //Autoload model using for all functions below
        $this->load->model('category_m');
    }

    public function index() {
        $this->all();
    }

    public function create() {
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $this->form_validation->set_rules('CategoryName', 'Tên loại sản phẩm', 'required|alpha|');
        $this->form_validation->set_rules('CategoryInfo', 'Category Info', 'required');
        $this->form_validation->set_rules('CategoryImage', 'Category Image', '');
        if ($this->form_validation->run() == FALSE) {
            $this->load->view('Manage/Category/create_form');
        } else {
            $data = array(
                'CategoryName' => $this->input->post('CategoryName'), //$_POST['CategoryName'],
                'CategoryInfo' => $this->input->post('CategoryInfo'), //$_POST['CategoryInfo'],
                'CategoryImage' => $this->input->post('CategoryImage')//$_POST['CategoryImage']
            );
            $this->category_m->create($data);
            header("Location: http://localhost/luyenga/category");
        }
    }

    public function create_result() {

        $data = array(
            'CategoryName' => $this->input->post('CategoryName'), //$_POST['CategoryName'],
            'CategoryInfo' => $this->input->post('CategoryInfo'), //$_POST['CategoryInfo'],
            'CategoryImage' => $this->input->post('CategoryImage')//$_POST['CategoryImage']
        );
        $this->category_m->create($data);
        header("Location: http://localhost/luyenga/category");
    }

    public function search($keyToSearch) {
        //abc.com/Category/search/$keyToSearch
        $data = array();
        $this->load->view('Manage/Category/search', $data);
    }

    public function all() {
        $data = array();
        $data['category'] = $this->category_m->all();
        $this->load->view('Manage/Category/list', $data);
    }

    public function delete($CategoryTracker) {
        //abc.com/Category/delete/$CategoryTracker
        $data = array();
        $this->load->view('Manage/Category/delete', $data);
    }

    public function edit($CategoryTracker) {
        //abc.com/Category/edit/$CategoryTracker
        $data = array();
        $this->load->view('Manage/Category/edit', $data);
    }

    public function detail($CategoryTracker) {
        //abc.com/Category/detail/$CategoryTracker
        $data = array();
        $this->load->view('Manage/Category/detail', $data);
    }

}
